package com.xzsoft.gdwz.HttpInterfaces;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import xz.facade.user.model.Result;

import com.xzsoft.gdwz.common.HttpClientUtil;
import com.xzsoft.gdwz.model.UserToken;
import com.xzsoft.gdwz.services.JdOrderService;
import com.xzsoft.gdwz.services.RefreshTokenServices;

/**
 * 京东接口
 * @author hellocworld
 *
 */
@Component
public class JingDongInf {
	
	@Autowired
	private RefreshTokenServices refreshTokenServices;
	
	@Autowired
	private JdOrderService jdOrderService;
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}
	
	
	
	
	/**
	 * 确认订单
	 * @param jdOrderId
	 * @return
	 */
	public boolean confirmOrder(String jdOrderId){
		UserToken token = refreshTokenServices.getToken("JD");
		String url = token.getConfirmOrderUrl();
		//String url = "http://localhost/service/oauth2/confirmOrder.do";
		Map<String,String> map = new HashMap<String,String>();
		String accessToken = token.getAccessToken();
		map.put("token", accessToken);  
		map.put("jdOrderId", jdOrderId);
		String result = HttpClientUtil.httpPost(url, map);
		JSONObject jsonObject = JSONObject.fromObject(result);
		boolean success = (Boolean)jsonObject.get("result");
		return success;
	}
	
	/**
	 * 取消订单
	 * @param jdOrderId
	 * @return
	 */
	public boolean cancleOrder(String jdOrderId){
		UserToken token = refreshTokenServices.getToken("JD");
		String url = token.getCancelOrderUrl();
		//String url = "http://localhost/service/oauth2/cancle.do";
		Map<String,String> map = new HashMap<String,String>();
		String accessToken = token.getAccessToken();
		map.put("token", accessToken);
		map.put("jdOrderId", jdOrderId);
		String result = HttpClientUtil.httpPost(url, map);
		JSONObject jsonObject = JSONObject.fromObject(result);
		boolean success = (Boolean)jsonObject.get("result");
		return success;
	}
	
	/**
	 * 查询订单
	 * @param jdOrderId
	 */
	public void selectJdOrder(String jdOrderId){
		UserToken token = refreshTokenServices.getToken("JD");
		Result returnResult = new Result();
		String selectOrderUrl = token.getSelectOrderUrl();
		String accessToken = token.getAccessToken();
		Map<String,String> map = new HashMap<String,String>();		
		map.put("token", accessToken);
		map.put("jdOrderId", jdOrderId);
		String result = HttpClientUtil.httpPost(selectOrderUrl, map);
		JSONObject jsonObject = JSONObject.fromObject(result);
		//将jsonObject转换为本地订单
		boolean success = (Boolean)jsonObject.get("success");
		if(success){
			JSONArray resultArray = (JSONArray)jsonObject.get("result");
			//resultMap.put("ecommerce_name", "JD");
			try {
				jdOrderService.saveOrder(resultArray);
			} catch (Exception e) {
				e.printStackTrace();
				returnResult.setResult("");
				returnResult.setResultCode("C0012");
				returnResult.setResultMessage(e.getMessage());
				returnResult.setSuccess(false);
			}
		}
	}
	
}
